/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package daoimpl;

import controller.ConnectionMySQL;
import dao.LoginDao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Login;

/**
 *
 * @author Rodrigo
 */
public class LoginDaoImpl implements LoginDao {
    
    @Override
    public void cadastrarLogin(Login l) {
        String sql = "Insert into login(usuario,senha) values (?,?) ";
        
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);
            ps.setString(1, l.getPerfil().name());
            ps.setString(2, l.getSenha());
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(LoginDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    @Override
    public void alterarLogin(Login l) {
        String sql = "Update login set senha=? WHERE id=? ";
        
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);
            ps.setString(1, l.getSenha());
            ps.setInt(2, l.getId());
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(LoginDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    @Override
    public Login retornaLogin(Login l) {
        String sql = "select * from login where usuario = ? ";
        Login login = new Login();
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);
            ps.setString(1, l.getPerfil().name());            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next()) {                
                login.setId(rs.getInt("id"));
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return login;
    }
    
    @Override
    public boolean isLoginValido(Login l) {
        Long qtd = null;
        String sql = "Select COUNT(*) as qtd from Login WHERE usuario = ? and senha = ?";
        
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);
            ps.setString(1, l.getPerfil().name());
            ps.setString(2, l.getSenha());
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next()) {
                qtd = rs.getLong("qtd");
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return qtd > 0;
        
    }
    
    @Override
    public boolean isLoginCadastrado(Login l) {
        Long qtd = null;
        String sql = "Select COUNT(*) as qtd from Login WHERE usuario = ? ";
        
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);
            ps.setString(1, l.getPerfil().name());
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next()) {
                qtd = rs.getLong("qtd");
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return qtd > 0;
        
    }
    
}
